package org.wdj.web;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

//第一个demo
@RestController             // 等同于 @Controller + @ResponseBody
@RequestMapping("hello")    // 表示这个类下所有 URL 为 /hello 加上注解在方法上的 RequestMapping 的值
public class HelloSpringBoot {
    //log4j2 日志
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WebApplication.class);

    @RequestMapping("string")
    @ResponseStatus(HttpStatus.OK)
    public String helloString() {
        log.trace("trace");
        log.debug("debug");
        log.warn("warn");
        log.info("info");
        log.error("error");
        return "hello spring boot";
    }

    @RequestMapping("json")
    public Messages helloJson() {
        //此处不需要构造函数，如果提示找不到函数，添加 lombok 插件
        return new Messages("success", "hello spring boot by json");
    }

    @GetMapping(value = "param")
    public Messages param(@RequestParam(value = "username", defaultValue = "boot") String username) {
        return new Messages("success", "hello "+username);
    }

    //path variable
    @PostMapping(value = "path/{username}")
    public Messages pathVariable(@PathVariable("username") String username) {
        return new Messages("success", "hello "+username);
    }

    @PostMapping("body")
    public Messages body(@RequestBody SysUser sysUser) {
        Messages messages = new Messages();
        if (sysUser.getUsername() != null && sysUser.getPassword() != null
                && sysUser.getUsername().equals("user") && sysUser.getPassword().equals("springboot")) {
            messages.setStatus("success");
            messages.setMessages("login success");
        } else {
            messages.setStatus("error");
            messages.setMessages("login error");
        }
        return messages;
    }

    @PostMapping("form")
    public Messages form(SysUser sysUser) {
        Messages messages = new Messages();
        if (sysUser.getUsername() != null && sysUser.getPassword() != null
                && sysUser.getUsername().equals("user") && sysUser.getPassword().equals("springboot")) {
            messages.setStatus("success");
            messages.setMessages("login success");
        } else {
            messages.setStatus("error");
            messages.setMessages("login error");
        }
        return messages;
    }
}
